import axios from "axios";
import { Loading, Toast } from 'react-vant';

let loading = true
const instance = axios.create(
  {
    baseURL: 'https://api-hmugo-web.itheima.net/api/public/v1', 
    // 所有的请求都会拼接一个baseurl这么一个路径
  }
);
instance.defaults.timeout = 2500;
// Add a request interceptor
instance.interceptors.request.use(function (config) {
     if (loading) {
         Toast.loading({message:"...loading"})
     }
    // Do something before request is sent
    return config;
  }, function (error) {
    // Do something with request error
    return Promise.reject(error);
  });

// Add a response interceptor
instance.interceptors.response.use(function (response) {
  loading = false
  console.log(loading);
    // Do something with response data
    return response;
  }, function (error) {
      if (error.response.status == "500") {
        Toast.fail('服务端错误')
      } else if (error.response.status == "502") {
        Toast.fail('接口找不到')
      } else if (error.response.status == "404") {
        Toast.fail('前端接口写错了')
      } else {
        Toast.fail('未知错误')
      }
    // Do something with response error
    return Promise.reject(error);
  });
export default instance